﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class updateData : System.Web.UI.Page
{
    bussiness bus;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bus = new bussiness();
            List<TypeEcard> lstTypeEcard = bus.getAllTypeEcard();
            ddlTypeCardName.DataSource = lstTypeEcard;
            ddlTypeCardName.DataTextField = "typeCardName";
            ddlTypeCardName.DataValueField = "typeEcardId";
            ddlTypeCardName.DataBind();
            load_data();
        }

    }
    public void load_data()
    {
        bus = new bussiness();
        List<Ecard> lstEcard =  bus.getAllEcard();
        GridView1.DataSource = lstEcard;
        GridView1.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bus = new bussiness();
        bool flag = true;
        string urlImage = txtUrlImage.Text;
        string urlFlash = txtUrlFlash.Text;
        string nameEcard = txtNameEcard.Text;
        int typeCardId = Convert.ToInt32(ddlTypeCardName.SelectedValue);
        if (urlImage == "" || urlFlash == "" || nameEcard == "")
        {
            flag = false;
            lblInfo.Text = "fields blank ";
        }
        else
        {
            List<Ecard> lstEcard = bus.getAllEcard();
            foreach(Ecard ecard in lstEcard)
            {
                if(ecard.UrlFlash == urlFlash)
                {
                    flag = false;
                    lblInfo.Text = "urlFlash is existed ";
                    break;
                }
            }
        }
        if(flag)
        {
            lblInfo.Text = "";
            Ecard ecard = new Ecard();
            ecard.UrlImage = urlImage;
            ecard.UrlFlash = urlFlash;
            ecard.EcardName = nameEcard;
            ecard.TypeId = typeCardId;
            bus.insertEcard(ecard);
            lblInfo.Text = "insert Ecard Successful";
            txtUrlImage.Text = "";
            txtUrlFlash.Text = "";
            txtNameEcard.Text = "";
        }
    }
}